﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent

Partial Class setup
    Inherits System.Web.UI.Page

    Private Sub LoadSetting()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Dim TempDT2 As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT CompanyName,CompanyDesc,CompanyLogo,CompanyInitial,RelistBoolean,Old,New, OldColor, NewColor,UpdateColor,TabBoolean,CloseFormat,OpenFormat,InvoiceFormat,CloseUseOpen,InvoiceUseClose,InvoiceUseOpen,ListingPercent,ReminderDeleteDays,IntActiveAnnouncement,IntActiveReminder,IntReminderRenew,StampDutyFrom,StampDutyTo" & _
                                     ",FeedbackColor0,FeedbackColor1,FeedbackColor2,FeedbackColor3,FeedbackColor4,FeedbackColor5,FeedbackColor6,FeedbackColor7,FeedbackColor8 " & _
                                     "FROM AW_Setting"
        objDB.FillData(TempDT, "Setting", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        For Each Readrow As Data.DataRow In TempDT.Rows
            txtCompanyName.Text = Readrow.Item("CompanyName").ToString
            txtCompanyDesc.Text = Readrow.Item("CompanyDesc").ToString
            If Readrow.Item("CompanyLogo").ToString <> "" Then
                imgCompanyLogo.ImageUrl = "avatar/" & Readrow.Item("CompanyLogo").ToString
                lblOldLogo.Text = Readrow.Item("CompanyLogo").ToString
            Else
                imgCompanyLogo.Visible = False
            End If
            txtCompanyInitial.Text = Readrow.Item("CompanyInitial").ToString
            'If Readrow.Item("RelistBoolean").ToString = "1" Then
            '    chkRelist.Checked = True
            'Else
            '    chkRelist.Checked = False
            'End If
            txtOldProperty.Text = Readrow.Item("Old").ToString
            txtNewProperty.Text = Readrow.Item("New").ToString
            txtOldColor.Text = Mid(Readrow.Item("OldColor").ToString, 2, Readrow.Item("OldColor").ToString.Length - 1)
            txtOldColor.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("OldColor").ToString)
            txtNewColor.Text = Mid(Readrow.Item("NewColor").ToString, 2, Readrow.Item("NewColor").ToString.Length - 1)
            txtNewColor.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("NewColor").ToString)
            txtCloseFormat.Text = ReturnFormat(Readrow.Item("CloseFormat").ToString)
            lblCloseFormat.Text = Readrow.Item("CloseFormat").ToString
            txtOpenFormat.Text = ReturnFormat(Readrow.Item("OpenFormat").ToString)
            lblOpenFormat.Text = Readrow.Item("OpenFormat").ToString
            txtInvoiceFormat.Text = ReturnFormat(Readrow.Item("InvoiceFormat").ToString)
            lblInvoiceFormat.Text = Readrow.Item("InvoiceFormat").ToString
            txtUpdatedPropertyColor.Text = Mid(Readrow.Item("UpdateColor").ToString, 2, Readrow.Item("UpdateColor").ToString.Length - 1)
            txtUpdatedPropertyColor.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("UpdateColor").ToString)

            'Edit by tanchinteong 20130109
            txtAgentFeedbackColor0.Text = Mid(Readrow.Item("FeedbackColor0").ToString, 2, Readrow.Item("FeedbackColor0").ToString.Length - 1)
            txtAgentFeedbackColor0.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor0").ToString)

            txtAgentFeedbackColor1.Text = Mid(Readrow.Item("FeedbackColor1").ToString, 2, Readrow.Item("FeedbackColor1").ToString.Length - 1)
            txtAgentFeedbackColor1.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor1").ToString)

            txtAgentFeedbackColor2.Text = Mid(Readrow.Item("FeedbackColor2").ToString, 2, Readrow.Item("FeedbackColor2").ToString.Length - 1)
            txtAgentFeedbackColor2.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor2").ToString)

            txtAgentFeedbackColor3.Text = Mid(Readrow.Item("FeedbackColor3").ToString, 2, Readrow.Item("FeedbackColor3").ToString.Length - 1)
            txtAgentFeedbackColor3.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor3").ToString)

            txtAgentFeedbackColor4.Text = Mid(Readrow.Item("FeedbackColor4").ToString, 2, Readrow.Item("FeedbackColor4").ToString.Length - 1)
            txtAgentFeedbackColor4.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor4").ToString)

            txtAgentFeedbackColor5.Text = Mid(Readrow.Item("FeedbackColor5").ToString, 2, Readrow.Item("FeedbackColor5").ToString.Length - 1)
            txtAgentFeedbackColor5.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor5").ToString)

            txtAgentFeedbackColor6.Text = Mid(Readrow.Item("FeedbackColor6").ToString, 2, Readrow.Item("FeedbackColor6").ToString.Length - 1)
            txtAgentFeedbackColor6.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor6").ToString)

            txtAgentFeedbackColor7.Text = Mid(Readrow.Item("FeedbackColor7").ToString, 2, Readrow.Item("FeedbackColor7").ToString.Length - 1)
            txtAgentFeedbackColor7.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor7").ToString)

            txtAgentFeedbackColor8.Text = Mid(Readrow.Item("FeedbackColor8").ToString, 2, Readrow.Item("FeedbackColor8").ToString.Length - 1)
            txtAgentFeedbackColor8.ForeColor = System.Drawing.ColorTranslator.FromHtml(Readrow.Item("FeedbackColor8").ToString)

            'txtStampDuty.Text = Readrow.Item("StampDuty").ToString

            'edited by minginn @ 8/5/11 - to take care of interval for reminder and announcement
            txtIntActiveAnnouncement.Text = Readrow.Item("IntActiveAnnouncement").ToString
            txtIntActiveReminder.Text = Readrow.Item("IntActiveReminder").ToString
            txtIntReminderRenew.Text = Readrow.Item("IntReminderRenew").ToString
            txtReminderDeleteDays.Text = Readrow.Item("ReminderDeleteDays").ToString

            If Readrow.Item("CloseUseOpen").ToString = "1" Then
                chkCloseUseOpen.Checked = True
            Else
                chkCloseUseOpen.Checked = False
            End If

            If Readrow.Item("InvoiceUseClose").ToString = "1" Then
                chkInvoiceUseClose.Checked = True
            Else
                chkInvoiceUseClose.Checked = False
            End If

            If Readrow.Item("InvoiceUseOpen").ToString = "1" Then
                chkInvoiceUseOpen.Checked = True
            Else
                chkInvoiceUseOpen.Checked = False
            End If

            txtListingFee.Text = Readrow.Item("ListingPercent").ToString

            'edited by minginn @ 13/9/11 - to allow user enter 2 sets of stampduty
            txtLessThan.Text = Readrow.Item("StampDutyFrom").ToString
            txtMoreThan.Text = Readrow.Item("StampDutyTo").ToString

        Next

        TempDT.Clear()
        TempDT.Reset()
        DeclareColumn(TempDT)
        DeclareColumn(TempDT2)
        GetStampDutyTable(objDB, TempDT, 1)
        GetStampDutyTable(objDB, TempDT2, 2)
        objDB.CloseConnection()
        repStampDuty.DataSource = TempDT
        repStampDuty.DataBind()
        ViewState("DataTable") = TempDT
        repStampDuty2.DataSource = TempDT2
        repStampDuty2.DataBind()
        ViewState("DataTable2") = TempDT2
        VisibilityRepStampDuty()
        VisibilityRepStampDuty2()
    End Sub

    Private Sub GetStampDutyTable(ByVal objDB As Database, ByRef TempDT As Data.DataTable, ByVal type As Integer)
        objDB.dbVariable.SQLString = "SELECT StampDutyId, AmountFrom, AmountTo, StampDuty, 'O' AS Status, '1' AS Visible FROM AW_StampDuty WHERE StampDutyType=@StampDutyType ORDER BY AmountFrom"
        objDB.AddParameter("StampDutyType", Data.SqlDbType.TinyInt, type)
        objDB.FillData(TempDT, "AW_StampDuty", Database.SQLCommandType.NormalString)
    End Sub

    Private Sub DeclareColumn(ByVal MyDT As Data.DataTable)
        Dim StampDutyId As Data.DataColumn = New Data.DataColumn("StampDutyId")
        StampDutyId.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(StampDutyId)
        Dim AmountFrom As Data.DataColumn = New Data.DataColumn("AmountFrom")
        AmountFrom.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(AmountFrom)
        Dim AmountTo As Data.DataColumn = New Data.DataColumn("AmountTo")
        AmountTo.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(AmountTo)
        Dim Commission As Data.DataColumn = New Data.DataColumn("StampDuty")
        Commission.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(Commission)
        Dim Status As Data.DataColumn = New Data.DataColumn("Status")
        Status.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Status)
        Dim Visible As Data.DataColumn = New Data.DataColumn("Visible")
        Visible.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Visible)
    End Sub

    Private Function ReturnFormat(ByVal FormatCode As String) As String

        Dim SplitedText As String() = FormatCode.Split(";")
        Dim CombinedString As String = String.Empty

        For Each objStr As String In SplitedText
            If objStr.Trim <> "" Then
                Select Case objStr.Trim
                    Case "tInit"
                        CombinedString = CombinedString & "Transactor Initial"
                    Case "lName"
                        CombinedString = CombinedString & "Lister Name"
                    Case "lInit"
                        CombinedString = CombinedString & "Lister Initial"
                    Case "cName"
                        CombinedString = CombinedString & "Company Name"
                    Case "cInit"
                        CombinedString = CombinedString & "Company Initial"
                    Case "rNumber"
                        CombinedString = CombinedString & "Running Number"
                    Case "dDate1"
                        CombinedString = CombinedString & "Date(dd/MM)"
                    Case "dDate2"
                        CombinedString = CombinedString & "Date(dd/MM/yyyy)"
                    Case "lType"
                        CombinedString = CombinedString & "List Type(R/S)"
                    Case "LoginInit"
                        CombinedString = CombinedString & "Login Initla"
                    Case "dash"
                        CombinedString = CombinedString & "-"
                    Case "slash"
                        CombinedString = CombinedString & "/"
                    Case "empty"
                        CombinedString = CombinedString & ""
                End Select
            End If
        Next

        Return CombinedString
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            LoadSetting()
            cbeUpdateStaff.ConfirmText = "Are you sure you want to update all staff Listing Percentage to " & txtListingFee.Text & "%?"
        End If
    End Sub

#Region " Document Format Related "

    Protected Sub btnPlus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlus.Click

        Dim TempdT As New Data.DataTable
        Dim NewRow As Data.DataRow
        TempdT.Columns.Add("Format")

        For nLoop As Integer = 0 To dlFormat.Items.Count - 1
            If CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue <> "remove" Then
                NewRow = TempdT.NewRow
                NewRow.Item("Format") = CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                TempdT.Rows.Add(NewRow)
            End If
        Next

        NewRow = TempdT.NewRow
        NewRow.Item("Format") = ""
        TempdT.Rows.Add(NewRow)
        ViewState.Item("Format") = TempdT
        dlFormat.DataSource = TempdT
        dlFormat.DataBind()
        mpeFormat.Show()
    End Sub

    Protected Sub dlFormat_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlFormat.ItemDataBound

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            CType(e.Item.FindControl("dlDDLFormat"), DropDownList).SelectedValue = CType(e.Item.FindControl("lblDLCurrentItem"), Label).Text
        End If
    End Sub

    Protected Sub imgCloseFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgCloseFormat.Click
        Dim TempDT As New Data.DataTable

        TempDT.Columns.Add("Format")

        If lblCloseFormat.Text <> "" Then
            Dim strFormat As String()

            strFormat = lblCloseFormat.Text.Split(";")

            Dim NewRow As Data.DataRow
            For Each objStr As String In strFormat
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = objStr
                TempDT.Rows.Add(NewRow)
            Next

        Else
            Dim NewRow As Data.DataRow


            For nLoop As Integer = 0 To 10
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = ""
                TempDT.Rows.Add(NewRow)
            Next
        End If

        ViewState.Add("Format", TempDT)
        dlFormat.DataSource = TempDT
        dlFormat.DataBind()
        lblWhichFormat.Text = "Close"
        mpeFormat.Show()

    End Sub

    Protected Sub btnCreateFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateFormat.Click
        Dim strFormat As String = String.Empty
        Dim strFormatValue As String = String.Empty

        If lblWhichFormat.Text = "Close" Then

            For nLoop As Integer = 0 To dlFormat.Items.Count - 1
                If CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue <> "remove" Then
                    'Edited by minginn @ 22/6/11 - to replace new format to the textbox
                    strFormat = strFormat & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    If strFormatValue = "" Then
                        strFormatValue = CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    Else
                        strFormatValue = strFormatValue & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    End If

                    'txtCloseFormat.Text = txtCloseFormat.Text & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    'lblCloseFormat.Text = lblCloseFormat.Text & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                End If
            Next
            txtCloseFormat.Text = strFormat
            lblCloseFormat.Text = strFormatValue
        ElseIf lblWhichFormat.Text = "Open" Then
            For nLoop As Integer = 0 To dlFormat.Items.Count - 1
                'Edited by minginn @ 22/6/11 - to replace new format to the textbox
                If CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue <> "remove" Then
                    strFormat = strFormat & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    If strFormatValue = "" Then
                        strFormatValue = CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    Else
                        strFormatValue = strFormatValue & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    End If

                    'txtOpenFormat.Text = txtOpenFormat.Text & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    'lblOpenFormat.Text = lblOpenFormat.Text & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                End If
            Next
            txtOpenFormat.Text = strFormat
            lblOpenFormat.Text = strFormatValue
        ElseIf lblWhichFormat.Text = "Invoice" Then
            For nLoop As Integer = 0 To dlFormat.Items.Count - 1
                If CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue <> "remove" Then
                    'Edited by minginn @ 22/6/11 - to replace new format to the textbox
                    strFormat = strFormat & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    If strFormatValue = "" Then
                        strFormatValue = CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    Else
                        strFormatValue = strFormatValue & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                    End If

                    'txtInvoiceFormat.Text = txtInvoiceFormat.Text & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedItem.Text
                    'lblInvoiceFormat.Text = lblInvoiceFormat.Text & ";" & CType(dlFormat.Items(nLoop).FindControl("dlDDLFormat"), DropDownList).SelectedValue
                End If
            Next
            txtInvoiceFormat.Text = strFormat
            lblInvoiceFormat.Text = strFormatValue
        End If
    End Sub

    Protected Sub imgOpenFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgOpenFormat.Click
        Dim TempDT As New Data.DataTable

        TempDT.Columns.Add("Format")

        If lblOpenFormat.Text <> "" Then
            Dim strFormat As String()

            strFormat = lblOpenFormat.Text.Split(";")

            Dim NewRow As Data.DataRow
            For Each objStr As String In strFormat
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = objStr
                TempDT.Rows.Add(NewRow)
            Next

        Else
            Dim NewRow As Data.DataRow


            For nLoop As Integer = 0 To 10
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = ""
                TempDT.Rows.Add(NewRow)
            Next

        End If
        


        ViewState.Add("Format", TempDT)
        dlFormat.DataSource = TempDT
        dlFormat.DataBind()
        lblWhichFormat.Text = "Open"
        mpeFormat.Show()
    End Sub

    Protected Sub imgInvoiceFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgInvoiceFormat.Click
        Dim TempDT As New Data.DataTable

        TempDT.Columns.Add("Format")


        If lblInvoiceFormat.Text <> "" Then
            Dim strFormat As String()

            strFormat = lblInvoiceFormat.Text.Split(";")

            Dim NewRow As Data.DataRow
            For Each objStr As String In strFormat
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = objStr
                TempDT.Rows.Add(NewRow)
            Next

        Else

            Dim NewRow As Data.DataRow


            For nLoop As Integer = 0 To 10
                NewRow = TempDT.NewRow
                NewRow.Item("Format") = ""
                TempDT.Rows.Add(NewRow)
            Next
        End If

        ViewState.Add("Format", TempDT)
        dlFormat.DataSource = TempDT
        dlFormat.DataBind()
        lblWhichFormat.Text = "Invoice"
        mpeFormat.Show()
    End Sub

#End Region

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click

        'Begin Updating AW_Setting
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim CompanyLogoImg As HttpPostedFile
        Dim UploadedCompanyLogoImg As String = ""
        Dim UploadPhotoError As String = ""


        Try
            If filCompanyLogo.HasFile Then
                CompanyLogoImg = filCompanyLogo.PostedFile
                If System.IO.Path.GetExtension(CompanyLogoImg.FileName).ToLower() <> ".jpg" And System.IO.Path.GetExtension(CompanyLogoImg.FileName).ToLower() <> ".png" And System.IO.Path.GetExtension(CompanyLogoImg.FileName).ToLower() <> ".png" Then
                    UploadPhotoError = UploadPhotoError & CompanyLogoImg.FileName & " can only be JPG,GIF and PNG format<br />"
                Else
                    Dim objImgComp As New ImageComp
                    UploadedCompanyLogoImg = objImgComp.UploadSingleImage(CompanyLogoImg, "avatar", False, 150)
                End If
            End If
        Catch ex As Exception
            UploadPhotoError = UploadPhotoError & filCompanyLogo.PostedFile.FileName & " has error with error message : " & ex.Message & "<br />"
        End Try

        If UploadPhotoError = "" Then
            Try
                objDB.OpenConnection()
                objDB.InitDbTrans()
                If UploadedCompanyLogoImg <> "" Then
                    objDB.dbVariable.SQLString = "UPDATE AW_Setting SET CompanyName=@CompanyName,CompanyDesc=@CompanyDesc,CompanyLogo=@CompanyLogo,CompanyInitial=@CompanyInitial,RelistBoolean=@RelistBoolean, " & _
                                                 "Old=@Old,New=@New,OldColor=@OldColor,NewColor=@NewColor,UpdateColor=@UpdateColor,TabBoolean=@TabBoolean,CloseFormat=@CloseFormat,OpenFormat=@OpenFormat," & _
                                                 "InvoiceFormat=@InvoiceFormat,CloseUseOpen=@CloseUseOpen,InvoiceUseClose=@InvoiceUseClose,InvoiceUseOpen=@InvoiceUseOpen,ListingPercent=@ListingPercent," & _
                                                 "ReminderDeleteDays=@ReminderDeleteDays,IntActiveAnnouncement=@IntActiveAnnouncement,IntActiveReminder=@IntActiveReminder,IntReminderRenew=@IntReminderRenew," & _
                                                 "StampDutyFrom=@StampDutyFrom,StampDutyTo=@StampDutyTo," & _
                                                "FeedbackColor0=@FeedbackColor0,FeedbackColor1=@FeedbackColor1,FeedbackColor2=@FeedbackColor2,FeedbackColor3=@FeedbackColor3,FeedbackColor4=@FeedbackColor4," & _
                                                "FeedbackColor5=@FeedbackColor5,FeedbackColor6=@FeedbackColor6,FeedbackColor7=@FeedbackColor7,FeedbackColor8=@FeedbackColor8"
                    objDB.AddParameter("CompanyLogo", Data.SqlDbType.NVarChar, UploadedCompanyLogoImg)
                Else
                    objDB.dbVariable.SQLString = "UPDATE AW_Setting SET CompanyName=@CompanyName,CompanyDesc=@CompanyDesc,CompanyInitial=@CompanyInitial,RelistBoolean=@RelistBoolean, " & _
                                                "Old=@Old,New=@New,OldColor=@OldColor,NewColor=@NewColor,UpdateColor=@UpdateColor,TabBoolean=@TabBoolean,CloseFormat=@CloseFormat," & _
                                                "OpenFormat=@OpenFormat,InvoiceFormat=@InvoiceFormat,CloseUseOpen=@CloseUseOpen,InvoiceUseClose=@InvoiceUseClose," & _
                                                "ReminderDeleteDays=@ReminderDeleteDays,InvoiceUseOpen=@InvoiceUseOpen,ListingPercent=@ListingPercent," & _
                                                "IntActiveAnnouncement=@IntActiveAnnouncement,IntActiveReminder=@IntActiveReminder,IntReminderRenew=@IntReminderRenew," & _
                                                "StampDutyFrom=@StampDutyFrom,StampDutyTo=@StampDutyTo," & _
                                                "FeedbackColor0=@FeedbackColor0,FeedbackColor1=@FeedbackColor1,FeedbackColor2=@FeedbackColor2,FeedbackColor3=@FeedbackColor3,FeedbackColor4=@FeedbackColor4," & _
                                                "FeedbackColor5=@FeedbackColor5,FeedbackColor6=@FeedbackColor6,FeedbackColor7=@FeedbackColor7,FeedbackColor8=@FeedbackColor8"
                End If

                objDB.AddParameter("CompanyName", Data.SqlDbType.NVarChar, txtCompanyName.Text.Trim)
                objDB.AddParameter("CompanyDesc", Data.SqlDbType.NVarChar, txtCompanyDesc.Text.Trim)
                objDB.AddParameter("CompanyInitial", Data.SqlDbType.NVarChar, txtCompanyInitial.Text.ToUpper.Trim)
                objDB.AddParameter("RelistBoolean", Data.SqlDbType.TinyInt, 0)
                objDB.AddParameter("FileCloseFormat", Data.SqlDbType.NVarChar, lblCloseFormat.Text.Trim)
                objDB.AddParameter("Old", Data.SqlDbType.Int, txtOldProperty.Text.Trim)
                objDB.AddParameter("New", Data.SqlDbType.Int, txtNewProperty.Text.Trim)
                objDB.AddParameter("OldColor", Data.SqlDbType.NVarChar, "#" & txtOldColor.Text.Trim)
                objDB.AddParameter("NewColor", Data.SqlDbType.NVarChar, "#" & txtNewColor.Text.Trim)
                objDB.AddParameter("UpdateColor", Data.SqlDbType.NVarChar, "#" & txtUpdatedPropertyColor.Text.Trim)

                'Added by tanchinteong 20130109
                objDB.AddParameter("FeedbackColor0", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor0.Text.Trim)
                objDB.AddParameter("FeedbackColor1", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor1.Text.Trim)
                objDB.AddParameter("FeedbackColor2", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor2.Text.Trim)
                objDB.AddParameter("FeedbackColor3", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor3.Text.Trim)
                objDB.AddParameter("FeedbackColor4", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor4.Text.Trim)
                objDB.AddParameter("FeedbackColor5", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor5.Text.Trim)
                objDB.AddParameter("FeedbackColor6", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor6.Text.Trim)
                objDB.AddParameter("FeedbackColor7", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor7.Text.Trim)
                objDB.AddParameter("FeedbackColor8", Data.SqlDbType.NVarChar, "#" & txtAgentFeedbackColor8.Text.Trim)

                'objDB.AddParameter("StampDuty", Data.SqlDbType.Decimal, txtStampDuty.Text.Trim)
                'If chkUseTab.Checked = True Then
                '    objDB.AddParameter("TabBoolean", Data.SqlDbType.TinyInt, 1)
                'Else
                objDB.AddParameter("TabBoolean", Data.SqlDbType.TinyInt, 0)
                'End If
                objDB.AddParameter("CloseFormat", Data.SqlDbType.NVarChar, lblCloseFormat.Text.Trim)
                objDB.AddParameter("OpenFormat", Data.SqlDbType.NVarChar, lblOpenFormat.Text.Trim)
                objDB.AddParameter("InvoiceFormat", Data.SqlDbType.NVarChar, lblInvoiceFormat.Text.Trim)

                If chkCloseUseOpen.Checked = True Then
                    objDB.AddParameter("CloseUseOpen", Data.SqlDbType.TinyInt, 1)
                Else
                    objDB.AddParameter("CloseUseOpen", Data.SqlDbType.TinyInt, 0)
                End If

                If chkInvoiceUseClose.Checked = True Then
                    objDB.AddParameter("InvoiceUseClose", Data.SqlDbType.TinyInt, 1)
                Else
                    objDB.AddParameter("InvoiceUseClose", Data.SqlDbType.TinyInt, 0)
                End If

                If chkInvoiceUseOpen.Checked = True Then
                    objDB.AddParameter("InvoiceUseOpen", Data.SqlDbType.TinyInt, 1)
                Else
                    objDB.AddParameter("InvoiceUseOpen", Data.SqlDbType.TinyInt, 0)
                End If
                'Edited by minginn @ 22/6/11 - handle the convert string to decimal error when user dun fill in anything
                If txtListingFee.Text = String.Empty Then
                    objDB.AddParameter("ListingPercent", Data.SqlDbType.Decimal, DBNull.Value)
                Else
                    objDB.AddParameter("ListingPercent", Data.SqlDbType.Decimal, txtListingFee.Text)
                End If
                'Edited by minginn @ 5/8/11 - to handle the interval for reminder and announcement
                If txtIntActiveAnnouncement.Text = String.Empty Then
                    objDB.AddParameter("IntActiveAnnouncement", Data.SqlDbType.Int, 0)
                Else
                    objDB.AddParameter("IntActiveAnnouncement", Data.SqlDbType.Int, CInt(txtIntActiveAnnouncement.Text))
                End If
                If txtIntActiveReminder.Text = String.Empty Then
                    objDB.AddParameter("IntActiveReminder", Data.SqlDbType.Int, 0)
                Else
                    objDB.AddParameter("IntActiveReminder", Data.SqlDbType.Int, CInt(txtIntActiveReminder.Text))
                End If
                If txtIntReminderRenew.Text = String.Empty Then
                    objDB.AddParameter("IntReminderRenew", Data.SqlDbType.Int, 0)
                Else
                    objDB.AddParameter("IntReminderRenew", Data.SqlDbType.Int, CInt(txtIntReminderRenew.Text))
                End If
                If txtReminderDeleteDays.Text = String.Empty Then
                    objDB.AddParameter("ReminderDeleteDays", Data.SqlDbType.TinyInt, 0)
                Else
                    objDB.AddParameter("ReminderDeleteDays", Data.SqlDbType.TinyInt, CInt(txtReminderDeleteDays.Text))
                End If
                If txtLessThan.Text = String.Empty Then
                    objDB.AddParameter("StampDutyFrom", Data.SqlDbType.Decimal, 0)
                Else
                    objDB.AddParameter("StampDutyFrom", Data.SqlDbType.Decimal, CDbl(txtMoreThan.Text.Trim))
                End If
                If txtMoreThan.Text = String.Empty Then
                    objDB.AddParameter("StampDutyTo", Data.SqlDbType.Decimal, 0)
                Else
                    objDB.AddParameter("StampDutyTo", Data.SqlDbType.Decimal, CDbl(txtMoreThan.Text.Trim))
                End If
                objDB.ExecuteQuery()
                AddStampDuty(objDB)
                AddStampDuty2(objDB)
                objDB.DbTransCommit()
                objDB.CloseConnection()
                If lblOldLogo.Text <> "" Then
                    Dim objImgComp As New ImageComp
                    objImgComp.DeleteSingleFile("avatar", lblOldLogo.Text)
                End If
                LoadSetting()
                lblMsgSuccessful.Text = "System Setup Saved"
                divSuccessful.Visible = True
                lblMsgSuccessful.Visible = True
            Catch ex As Exception
                If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                    objDB.DbTransRollback()
                    objDB.CloseConnection()
                End If
                lblMsgError.Text = ex.Message
                divError.Visible = True
                If UploadedCompanyLogoImg <> "" Then
                    Dim objImgComp As New ImageComp
                    objImgComp.DeleteSingleFile("avatar", UploadedCompanyLogoImg)
                End If
            End Try
        Else
            lblMsgError.Text = UploadPhotoError
            divError.Visible = True
        End If

    End Sub

    Protected Sub chkCloseUseOpen_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkCloseUseOpen.CheckedChanged

        If chkCloseUseOpen.Checked = True Then
            imgCloseFormat.Visible = False
        Else
            imgCloseFormat.Visible = True
        End If
    End Sub

    Protected Sub chkInvoiceUseClose_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkInvoiceUseClose.CheckedChanged

        If chkInvoiceUseClose.Checked = True Then
            imgInvoiceFormat.Visible = False
            chkInvoiceUseOpen.Checked = False
        Else
            If chkInvoiceUseOpen.Checked = False Then
                imgInvoiceFormat.Visible = True
            Else
                imgInvoiceFormat.Visible = False
            End If
        End If
    End Sub

    Protected Sub chkInvoiceUseOpen_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkInvoiceUseOpen.CheckedChanged

        If chkInvoiceUseOpen.Checked = True Then
            imgInvoiceFormat.Visible = False
            chkInvoiceUseClose.Checked = False
        Else
            If chkInvoiceUseClose.Checked = False Then
                imgInvoiceFormat.Visible = True
            Else
                imgInvoiceFormat.Visible = False
            End If
        End If
    End Sub

    Protected Sub btnUpdateStaff_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpdateStaff.Click

        'Begin Updating All Staff Information
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "UPDATE AW_Staff SET ListingPercent=@ListingPercent WHERE StatusDelete=0"
        objDB.AddParameter("ListingPercent", Data.SqlDbType.Decimal, CDbl(txtListingFee.Text))
        objDB.ExecuteQuery()
        objDB.CloseConnection()

        divSuccessful.Visible = True
        lblMsgSuccessful.Text = "Staff Listing Percentage updated"
        lblMsgSuccessful.Visible = True
    End Sub

    Protected Sub btnAddStampDuty_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddStampDuty.Click
        If txtAmountFrom.Text <> "" And txtAmountTo.Text <> "" And txtStampDuty.Text <> "" Then
            AddNewRow()
            txtAmountFrom.Text = ""
            txtAmountTo.Text = ""
            txtStampDuty.Text = ""
        Else
            'do something
        End If
    End Sub

    Protected Sub btnAddStampDuty2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddStampDuty2.Click
        If txtAmountFrom2.Text <> "" And txtAmountTo2.Text <> "" And txtStampDuty2.Text <> "" Then
            AddNewRow2()
            txtAmountFrom2.Text = ""
            txtAmountTo2.Text = ""
            txtStampDuty2.Text = ""
        Else
            'do something
        End If
    End Sub

    Private Sub AddNewRow()
        Dim MyDT As New Data.DataTable

        If ViewState("DataTable") Is Nothing Then
            DeclareColumn(MyDT)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtAmountFrom.Text.Trim
            MyRow(2) = txtAmountTo.Text.Trim
            MyRow(3) = txtStampDuty.Text.Trim
            MyRow(4) = "N"
            MyRow(5) = "1"
            MyDT.Rows.Add(MyRow)
        Else
            MyDT = CType(ViewState("DataTable"), Data.DataTable)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtAmountFrom.Text.Trim
            MyRow(2) = txtAmountTo.Text.Trim
            MyRow(3) = txtStampDuty.Text.Trim
            MyRow(4) = "N"
            MyRow(5) = "1"
            MyDT.Rows.Add(MyRow)
        End If
        ViewState("DataTable") = MyDT
        repStampDuty.DataSource = MyDT
        repStampDuty.DataBind()
        VisibilityRepStampDuty()
    End Sub

    Private Sub AddNewRow2()
        Dim MyDT As New Data.DataTable

        If ViewState("DataTable2") Is Nothing Then
            DeclareColumn(MyDT)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtAmountFrom2.Text.Trim
            MyRow(2) = txtAmountTo2.Text.Trim
            MyRow(3) = txtStampDuty2.Text.Trim
            MyRow(4) = "N"
            MyRow(5) = "1"
            MyDT.Rows.Add(MyRow)
        Else
            MyDT = CType(ViewState("DataTable2"), Data.DataTable)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtAmountFrom2.Text.Trim
            MyRow(2) = txtAmountTo2.Text.Trim
            MyRow(3) = txtStampDuty2.Text.Trim
            MyRow(4) = "N"
            MyRow(5) = "1"
            MyDT.Rows.Add(MyRow)
        End If
        ViewState("DataTable2") = MyDT
        repStampDuty2.DataSource = MyDT
        repStampDuty2.DataBind()
        VisibilityRepStampDuty2()
    End Sub

    Protected Sub repStampDuty_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repStampDuty.ItemCommand
        If e.CommandName.ToUpper = "SAVE" Then
            Dim MyDT As New Data.DataTable
            'Dim MyRow() As Data.DataRow

            MyDT = CType(ViewState("DataTable"), Data.DataTable)
            'MyRow = MyDT.Select("CommissionId = '" & CType(repCommission.Items(e.CommandArgument).FindControl("lblhidRepCommissionId"), Label).Text & "'")
            'If MyRow IsNot Nothing AndAlso MyRow.Length > 0 Then
            '    For i As Integer = 0 To MyRow.Length - 1
            MyDT.Rows(e.CommandArgument)(0) = CType(repStampDuty.Items(e.CommandArgument).FindControl("lblhidRepStampDutyId"), Label).Text
            MyDT.Rows(e.CommandArgument)(1) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(2) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(3) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(4) = MyDT.Rows(e.CommandArgument)(4).ToString
            MyDT.Rows(e.CommandArgument)(5) = MyDT.Rows(e.CommandArgument)(5).ToString
            'Next
            MyDT.AcceptChanges()
            'End If
            ViewState("DataTable") = MyDT
            repStampDuty.DataSource = MyDT
            repStampDuty.DataBind()
            VisibilityRepStampDuty()
        ElseIf e.CommandName.ToUpper = "CANCEL" Then
            Dim lbtRepAmountFrom, lbtRepAmountTo, lbtRepStampDuty As LinkButton
            Dim txtRepAmountFrom, txtRepAmountTo, txtRepStampDuty As TextBox
            Dim btnMinusStampDuty, btnSaveStampDuty, btnCancelStampDuty As Button
            lbtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountFrom"), LinkButton)
            lbtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountTo"), LinkButton)
            lbtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepStampDuty"), LinkButton)
            txtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom"), TextBox)
            txtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo"), TextBox)
            txtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty"), TextBox)
            btnMinusStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnMinusStampDuty"), Button)
            btnSaveStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnSaveStampDuty"), Button)
            btnCancelStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnCancelStampDuty"), Button)
            txtRepAmountFrom.Visible = False
            txtRepAmountTo.Visible = False
            txtRepStampDuty.Visible = False
            btnSaveStampDuty.Visible = False
            btnCancelStampDuty.Visible = False
            lbtRepAmountFrom.Visible = True
            lbtRepAmountTo.Visible = True
            lbtRepStampDuty.Visible = True
            btnMinusStampDuty.Visible = True
        ElseIf e.CommandName.ToUpper = "MINUS" Then
            'Dim MyDT As New Data.DataTable
            'Dim MyRow() As Data.DataRow

            'MyDT = CType(ViewState("DataTable"), Data.DataTable)
            'MyRow = MyDT.Select("StampDutyId = '" & CType(repStampDuty.Items(e.CommandArgument).FindControl("lblhidRepStampDutyId"), Label).Text & "'")
            'If MyRow IsNot Nothing AndAlso MyRow.Length > 0 Then
            '    For i As Integer = 0 To MyRow.Length - 1
            '        MyRow(i).Delete()
            '    Next
            '    MyDT.AcceptChanges()
            'End If
            'ViewState("DataTable") = MyDT
            'repStampDuty.DataSource = MyDT
            'repStampDuty.DataBind()
            Dim MyDT As New Data.DataTable
            MyDT = CType(ViewState("DataTable"), Data.DataTable)
            MyDT.Rows(e.CommandArgument)(0) = MyDT.Rows(e.CommandArgument)(0).ToString
            MyDT.Rows(e.CommandArgument)(1) = MyDT.Rows(e.CommandArgument)(1).ToString
            MyDT.Rows(e.CommandArgument)(2) = MyDT.Rows(e.CommandArgument)(2).ToString
            MyDT.Rows(e.CommandArgument)(3) = MyDT.Rows(e.CommandArgument)(3).ToString
            MyDT.Rows(e.CommandArgument)(4) = MyDT.Rows(e.CommandArgument)(4).ToString
            MyDT.Rows(e.CommandArgument)(5) = "0"
            'Next
            MyDT.AcceptChanges()
            'End If
            ViewState("DataTable") = MyDT
            repStampDuty.DataSource = MyDT
            repStampDuty.DataBind()
            VisibilityRepStampDuty()
        ElseIf e.CommandName.ToUpper = "EDIT" Then
            Dim lbtRepAmountFrom, lbtRepAmountTo, lbtRepStampDuty As LinkButton
            Dim txtRepAmountFrom, txtRepAmountTo, txtRepStampDuty As TextBox
            Dim btnMinusStampDuty, btnSaveStampDuty, btnCancelStampDuty As Button
            lbtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountFrom"), LinkButton)
            lbtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountTo"), LinkButton)
            lbtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepStampDuty"), LinkButton)
            txtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom"), TextBox)
            txtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo"), TextBox)
            txtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty"), TextBox)
            btnMinusStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnMinusStampDuty"), Button)
            btnSaveStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnSaveStampDuty"), Button)
            btnCancelStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnCancelStampDuty"), Button)
            txtRepAmountFrom.Visible = True
            txtRepAmountTo.Visible = True
            txtRepStampDuty.Visible = True
            btnSaveStampDuty.Visible = True
            btnCancelStampDuty.Visible = True
            lbtRepAmountFrom.Visible = False
            lbtRepAmountTo.Visible = False
            lbtRepStampDuty.Visible = False
            btnMinusStampDuty.Visible = False
        End If
    End Sub

    Protected Sub repStampDuty2_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repStampDuty2.ItemCommand
        If e.CommandName.ToUpper = "SAVE" Then
            Dim MyDT As New Data.DataTable
            'Dim MyRow() As Data.DataRow

            MyDT = CType(ViewState("DataTable2"), Data.DataTable)
            'MyRow = MyDT.Select("CommissionId = '" & CType(repCommission.Items(e.CommandArgument).FindControl("lblhidRepCommissionId"), Label).Text & "'")
            'If MyRow IsNot Nothing AndAlso MyRow.Length > 0 Then
            '    For i As Integer = 0 To MyRow.Length - 1
            MyDT.Rows(e.CommandArgument)(0) = CType(repStampDuty.Items(e.CommandArgument).FindControl("lblhidRepStampDutyId2"), Label).Text
            MyDT.Rows(e.CommandArgument)(1) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom2"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(2) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo2"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(3) = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty2"), TextBox).Text
            MyDT.Rows(e.CommandArgument)(4) = MyDT.Rows(e.CommandArgument)(4).ToString
            MyDT.Rows(e.CommandArgument)(5) = MyDT.Rows(e.CommandArgument)(5).ToString
            'Next
            MyDT.AcceptChanges()
            'End If
            ViewState("DataTable2") = MyDT
            repStampDuty2.DataSource = MyDT
            repStampDuty2.DataBind()
            VisibilityRepStampDuty2()
        ElseIf e.CommandName.ToUpper = "CANCEL" Then
            Dim lbtRepAmountFrom, lbtRepAmountTo, lbtRepStampDuty As LinkButton
            Dim txtRepAmountFrom, txtRepAmountTo, txtRepStampDuty As TextBox
            Dim btnMinusStampDuty, btnSaveStampDuty, btnCancelStampDuty As Button
            lbtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountFrom2"), LinkButton)
            lbtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountTo2"), LinkButton)
            lbtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepStampDuty2"), LinkButton)
            txtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom2"), TextBox)
            txtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo2"), TextBox)
            txtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty2"), TextBox)
            btnMinusStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnMinusStampDuty2"), Button)
            btnSaveStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnSaveStampDuty2"), Button)
            btnCancelStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnCancelStampDuty2"), Button)
            txtRepAmountFrom.Visible = False
            txtRepAmountTo.Visible = False
            txtRepStampDuty.Visible = False
            btnSaveStampDuty.Visible = False
            btnCancelStampDuty.Visible = False
            lbtRepAmountFrom.Visible = True
            lbtRepAmountTo.Visible = True
            lbtRepStampDuty.Visible = True
            btnMinusStampDuty.Visible = True
        ElseIf e.CommandName.ToUpper = "MINUS" Then
            'Dim MyDT As New Data.DataTable
            'Dim MyRow() As Data.DataRow

            'MyDT = CType(ViewState("DataTable"), Data.DataTable)
            'MyRow = MyDT.Select("StampDutyId = '" & CType(repStampDuty.Items(e.CommandArgument).FindControl("lblhidRepStampDutyId"), Label).Text & "'")
            'If MyRow IsNot Nothing AndAlso MyRow.Length > 0 Then
            '    For i As Integer = 0 To MyRow.Length - 1
            '        MyRow(i).Delete()
            '    Next
            '    MyDT.AcceptChanges()
            'End If
            'ViewState("DataTable") = MyDT
            'repStampDuty.DataSource = MyDT
            'repStampDuty.DataBind()
            Dim MyDT As New Data.DataTable
            MyDT = CType(ViewState("DataTable2"), Data.DataTable)
            MyDT.Rows(e.CommandArgument)(0) = MyDT.Rows(e.CommandArgument)(0).ToString
            MyDT.Rows(e.CommandArgument)(1) = MyDT.Rows(e.CommandArgument)(1).ToString
            MyDT.Rows(e.CommandArgument)(2) = MyDT.Rows(e.CommandArgument)(2).ToString
            MyDT.Rows(e.CommandArgument)(3) = MyDT.Rows(e.CommandArgument)(3).ToString
            MyDT.Rows(e.CommandArgument)(4) = MyDT.Rows(e.CommandArgument)(4).ToString
            MyDT.Rows(e.CommandArgument)(5) = "0"
            'Next
            MyDT.AcceptChanges()
            'End If
            ViewState("DataTable2") = MyDT
            repStampDuty2.DataSource = MyDT
            repStampDuty2.DataBind()
            VisibilityRepStampDuty2()
        ElseIf e.CommandName.ToUpper = "EDIT" Then
            Dim lbtRepAmountFrom, lbtRepAmountTo, lbtRepStampDuty As LinkButton
            Dim txtRepAmountFrom, txtRepAmountTo, txtRepStampDuty As TextBox
            Dim btnMinusStampDuty, btnSaveStampDuty, btnCancelStampDuty As Button
            lbtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountFrom2"), LinkButton)
            lbtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepAmountTo2"), LinkButton)
            lbtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("lbtRepStampDuty2"), LinkButton)
            txtRepAmountFrom = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountFrom2"), TextBox)
            txtRepAmountTo = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepAmountTo2"), TextBox)
            txtRepStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("txtRepStampDuty2"), TextBox)
            btnMinusStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnMinusStampDuty2"), Button)
            btnSaveStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnSaveStampDuty2"), Button)
            btnCancelStampDuty = CType(repStampDuty.Items(e.CommandArgument).FindControl("btnCancelStampDuty2"), Button)
            txtRepAmountFrom.Visible = True
            txtRepAmountTo.Visible = True
            txtRepStampDuty.Visible = True
            btnSaveStampDuty.Visible = True
            btnCancelStampDuty.Visible = True
            lbtRepAmountFrom.Visible = False
            lbtRepAmountTo.Visible = False
            lbtRepStampDuty.Visible = False
            btnMinusStampDuty.Visible = False
        End If
    End Sub

    Private Function AddStampDuty(ByVal objdb As Database) As Integer
        If txtAmountFrom.Text <> "" Or txtAmountTo.Text <> "" Or txtStampDuty.Text <> "" Then
            AddNewRow()
        End If
        Dim myDT As New Data.DataTable
        myDT = CType(ViewState("DataTable"), Data.DataTable)
        If Not myDT Is Nothing Then
            For Each myrow As Data.DataRow In myDT.Rows
                If myrow.Item("Status") = "N" And myrow.Item("Visible") = "1" Then
                    'insert
                    objdb.dbVariable.SQLString = "INSERT INTO AW_StampDuty (StampDutyId,AmountFrom,AmountTo,StampDuty,StampDutyType) " & _
                    "VALUES (@StampDutyId,@AmountFrom,@AmountTo,@StampDuty,@StampDutyType)"
                    objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                    objdb.AddParameter("AmountFrom", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountFrom")))
                    objdb.AddParameter("AmountTo", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountTo")))
                    objdb.AddParameter("StampDuty", Data.SqlDbType.Decimal, CDec(myrow.Item("StampDuty")))
                    objdb.AddParameter("StampDutyType", Data.SqlDbType.TinyInt, 1)
                    objdb.ExecuteQuery()
                Else
                    If myrow.Item("Visible") = "0" Then
                        'delete
                        objdb.dbVariable.SQLString = "DELETE AW_StampDuty WHERE StampDutyId=@StampDutyId"
                        objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                        objdb.ExecuteQuery()
                    Else
                        'update
                        objdb.dbVariable.SQLString = "UPDATE AW_StampDuty SET AmountFrom=@AmountFrom, AmountTo=@AmountTo, StampDuty=@StampDuty " & _
                        "WHERE StampDutyId=@StampDutyId"
                        objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                        objdb.AddParameter("AmountFrom", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountFrom")))
                        objdb.AddParameter("AmountTo", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountTo")))
                        objdb.AddParameter("StampDuty", Data.SqlDbType.Decimal, CDec(myrow.Item("StampDuty")))
                        objdb.ExecuteQuery()
                    End If
                End If
            Next
            Return 1
        Else
            lblMsgError.Text = "Please fill in the stamp duty"
            Return 0
        End If

    End Function

    Private Function AddStampDuty2(ByVal objdb As Database) As Integer
        If txtAmountFrom2.Text <> "" Or txtAmountTo2.Text <> "" Or txtStampDuty2.Text <> "" Then
            AddNewRow2()
        End If
        Dim myDT As New Data.DataTable
        myDT = CType(ViewState("DataTable2"), Data.DataTable)
        If Not myDT Is Nothing Then
            For Each myrow As Data.DataRow In myDT.Rows
                If myrow.Item("Status") = "N" And myrow.Item("Visible") = "1" Then
                    'insert
                    objdb.dbVariable.SQLString = "INSERT INTO AW_StampDuty (StampDutyId,AmountFrom,AmountTo,StampDuty,StampDutyType) " & _
                    "VALUES (@StampDutyId,@AmountFrom,@AmountTo,@StampDuty,@StampDutyType)"
                    objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                    objdb.AddParameter("AmountFrom", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountFrom")))
                    objdb.AddParameter("AmountTo", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountTo")))
                    objdb.AddParameter("StampDuty", Data.SqlDbType.Decimal, CDec(myrow.Item("StampDuty")))
                    objdb.AddParameter("StampDutyType", Data.SqlDbType.TinyInt, 2)
                    objdb.ExecuteQuery()
                Else
                    If myrow.Item("Visible") = "0" Then
                        'delete
                        objdb.dbVariable.SQLString = "DELETE AW_StampDuty WHERE StampDutyId=@StampDutyId"
                        objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                        objdb.ExecuteQuery()
                    Else
                        'update
                        objdb.dbVariable.SQLString = "UPDATE AW_StampDuty SET AmountFrom=@AmountFrom, AmountTo=@AmountTo, StampDuty=@StampDuty " & _
                        "WHERE StampDutyId=@StampDutyId"
                        objdb.AddParameter("StampDutyId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("StampDutyId").ToString))
                        objdb.AddParameter("AmountFrom", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountFrom")))
                        objdb.AddParameter("AmountTo", Data.SqlDbType.Decimal, CDec(myrow.Item("AmountTo")))
                        objdb.AddParameter("StampDuty", Data.SqlDbType.Decimal, CDec(myrow.Item("StampDuty")))
                        objdb.ExecuteQuery()
                    End If
                End If
            Next
            Return 1
        Else
            lblMsgError.Text = "Please fill in the stamp duty"
            Return 0
        End If

    End Function

    Private Sub VisibilityRepStampDuty()
        For Each repitem As RepeaterItem In repStampDuty.Items
            If CType(repitem.FindControl("lblhidRepVisible"), Label).Text = "0" Then
                repitem.Visible = False
            Else
                repitem.Visible = True
            End If
        Next
    End Sub

    Private Sub VisibilityRepStampDuty2()
        For Each repitem As RepeaterItem In repStampDuty2.Items
            If CType(repitem.FindControl("lblhidRepVisible2"), Label).Text = "0" Then
                repitem.Visible = False
            Else
                repitem.Visible = True
            End If
        Next
    End Sub

End Class
